package nio.socket;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;

/**
 * @program JavaBase
 * @description:
 * @author: zhanglu
 * @create: 2019-11-22 13:51:00
 */
public class PipeSocket {

    public static void main(String[] args) throws IOException {

        Pipe pipe = Pipe.open();

        Pipe.SinkChannel channel = pipe.sink();

        ByteBuffer buf = ByteBuffer.allocate(1024);

        buf.put("通过管道发送数据".getBytes());
        buf.flip();

        channel.write(buf);

        Pipe.SourceChannel source = pipe.source();

        buf.flip();
        int len = source.read(buf);
        System.out.println(new String(buf.array(), 0, len));

        source.close();
        channel.close();

    }

}
